package hub.cathome.service.impl;

import hub.annotation.MayNotUse;
import hub.cathome.entity.Comment;
import hub.cathome.entity.ResourceLink;
import hub.cathome.entity.User;
import hub.cathome.mapper.CommentMapper;
import hub.cathome.mapper.ResourceLinkMapper;
import hub.cathome.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("commentService")
public class CommentServiceImpl implements CommentService {
    @Resource(name="commentMapper")
    private CommentMapper commentMapper;
    @Resource(name="resourceLinkMapper")
    private ResourceLinkMapper resourceLinkMapper;
    @Override
    public int insertComment(Comment comment) {
        return commentMapper.insertComment(comment);
    }

    @Override
    public int updateComment(Comment comment) {
        return commentMapper.updateComment(comment);
    }

    @Override
    public int deleteComment(Integer id) {
        return commentMapper.deleteComment(id);
    }

    @Override
    public List<Comment> getCommentByShotBlogId(Integer shotBlogId) {
        List<Comment> comments = commentMapper.getCommentByShotBlogId(shotBlogId);
        return getComments(comments);
    }

    @Override
    public Comment getCommentById(Integer id) {
        Comment comment = commentMapper.getCommentById(id);
        User u = comment.getUser();
        List<ResourceLink> rs = resourceLinkMapper.getResourceLinkByUserId(u.getId());
        if(rs.size()>0){
            u.setIcon(rs.get(0));
        }
        return comment;
    }
    public List<Comment> AddIconForComment(List<Comment> comments){
        return getComments(comments);
    }

    private List<Comment> getComments(List<Comment> comments) {
        for(Comment comment:comments){
            User u = comment.getUser();
            List<ResourceLink> rs = resourceLinkMapper.getResourceLinkByUserId(u.getId());
            if(rs.size()>0){
                u.setIcon(rs.get(0));
            }
        }
        return comments;
    }

    public Comment AddIconForComment(Comment comment){
        User u = comment.getUser();
        List<ResourceLink> rs = resourceLinkMapper.getResourceLinkByUserId(u.getId());
        if(rs.size()>0){
            u.setIcon(rs.get(0));
        }
        return comment;
    }
}
